package servlet.userRole;

import bean.dto.RolesDTO;
import bean.dto.UserRoleDTO;
import bean.dto.UserRoleQueryPage;
import bean.pojo.Permissions;
import bean.vo.UserRoleVo;
import com.alibaba.fastjson.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.r.R;
import service.RolesService;
import service.UsersRoleService;
import service.UsersService;
import service.impl.RolesServiceImpl;
import service.impl.UsersRoleServiceImpl;
import service.impl.UsersServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.List;

/**
 * servlet.user
 * User: mcw
 * Date: 2024/12/18 下午7:32
 * motto:   春天（spring）没有虫子（bug)
 * Description:
 * Version: V1.0
 */
@WebServlet(name = "UserRoleServlet", value ="/userRole/*")
public class UserRoleServlet extends BaseServlet {
    /**
     * 权限分页查询
     * @param request
     * @return
     */
    public R findByPage(HttpServletRequest request) {
        String str_page = request.getParameter("page");
        System.out.println("PermissionManagement:page" + str_page);
        String str_limit = request.getParameter("limit");
        System.out.println("PermissionManagement:limit" + str_limit);
        UserRoleQueryPage queryPage = new UserRoleQueryPage();
        if (str_page.trim().length() > 0) {
            try {
                queryPage.setPage(Integer.parseInt(str_page));
            } catch (Exception e) {
                queryPage.setPage(1);
            }
        }
        //设置每页行数
        if (str_limit.trim().length() > 0) {
            try {
                queryPage.setLimit(Integer.parseInt(str_limit));
            } catch (Exception e) {
                queryPage.setLimit(10);
            }
        }
        //设置查询条件
        queryPage.setRoleId(NumberFormat.stringToInteger(request.getParameter("roleId")));
        queryPage.setUserName(request.getParameter("userName"));

        System.out.println("UserRoleServlet:findByPage:queryPage:"+queryPage);
        UsersRoleService usersRoleService = new UsersRoleServiceImpl();
        //满足条件的总条数
        Long  total=usersRoleService.findByTotal(queryPage);
        System.out.println("UserRoleServlet:findByPage:total:"+total);
        //当前页码的数据
        List<UserRoleVo> list=null;
        if(total>0) {
            //当前页码的数据
            list = usersRoleService.findByPage(queryPage);
            System.out.println("UserRoleServlet:findByPage:list:"+list.toString());
        }
        //Data?     Long       addData把data HashMap
        R  r= R.ok().addData("total",total).addData("list",list);
        return r;
    }
    /**
     * 添加用户以及用户角色
     *
     * @param request
     * @return
     */
    public R addUserRole(HttpServletRequest request) throws Exception {
        //获取请求流
        BufferedReader bufferedReader = request.getReader();
        String req_data = bufferedReader.readLine();

        UserRoleDTO userRoleDTO = JSONObject.parseObject(req_data, UserRoleDTO.class);
        System.out.println("userRoleDTO:"+userRoleDTO);
       UsersRoleService usersRoleService = new UsersRoleServiceImpl();

        boolean update = usersRoleService.insert(userRoleDTO);
        R r = !update ? R.error("添加失败") : R.ok();
        return r;
    }

    /**
     * 根据id删除用户
     *
     * @param request
     * @return
     */
    public R deleteUserRole(HttpServletRequest request) {
        String id = request.getParameter("userId");
        System.out.println(id);
        R r;
        Integer userId = null;
        try {
            userId = Integer.parseInt(id);
            UsersRoleService usersRoleService = new UsersRoleServiceImpl();
            boolean update = usersRoleService.deleteUserRole(userId);
            r = !update ? R.error("删除失败") : R.ok();
        } catch (Exception e) {
            r = R.error("ID编号不正确");
        }
        return r;
    }

    /**
     * 修改用户或用户角色
     *
     * @param request
     * @return
     */
    public R updateUserRole(HttpServletRequest request) throws Exception {
        //获取请求流
        BufferedReader bufferedReader = request.getReader();
        String req_data = bufferedReader.readLine();

        UserRoleDTO userRoleDTO = JSONObject.parseObject(req_data, UserRoleDTO.class);
        System.out.println("userRoleDTO:"+userRoleDTO);
        UsersRoleService usersRoleService = new UsersRoleServiceImpl();

        boolean update = usersRoleService.updateUserRole(userRoleDTO);
        R r = !update ? R.error("修改失败") : R.ok();
        return r;
    }
}
